<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getPetAcquisitionOrders">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表
		v-loading="listLoading" 是否显示加载圈
		@selection-change=  选择发生改变都会触发事件
		-->
		<el-table :data="petAcquisitionOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<!--用来做多选的，以便批量删除-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<!--索引值-->
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="标识" width="100"  sortable>
			</el-table-column>
			<el-table-column prop="dirPath" label="路径" width="100" sortable>
			</el-table-column>
            <!--
                格式化有两方式：
                   1 :formatter不能加样式 :formatter="formatState"
                   2 <template scope="scope">
            -->
			<el-table-column prop="state" label="状态" width="120"  sortable>
                <template scope="scope">
                    <span v-if="scope.row.state==0" style="color: green">启用</span>
                    <span v-else style="color: red">禁用</span>
                </template>
			</el-table-column>
            <!--private Employee manager;-->
            <!--private PetAcquisitionOrder parent;-->
			<el-table-column prop="manager.username" label="经理" width="100" sortable>
			</el-table-column>
			<el-table-column prop="parent.name" label="上级收购订单" min-width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面 v-model="petAcquisitionOrderFormVisible" -->
		<el-dialog title="新增或编辑" :visible.sync="petAcquisitionOrderFormVisible" :close-on-click-modal="false">
			<!--:rules 校验-->
			<el-form :model="petAcquisitionOrderForm" label-width="80px" :rules="petAcquisitionOrderFormRules" ref="petAcquisitionOrderForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="petAcquisitionOrderForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="标识" prop="sn">
					<el-input v-model="petAcquisitionOrderForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="路径" prop="dirPath">
					<el-input v-model="petAcquisitionOrderForm.dirPath" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="petAcquisitionOrderForm.state">
						<el-radio class="radio" :label="0">正常</el-radio>
						<el-radio class="radio" :label="-1">停用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="收购订单经理" prop="manager">
					<!--<el-input v-model="petAcquisitionOrderForm.manager" auto-complete="off"></el-input>-->

					<!--:label 使用用来显示的-->
					<!--:value="item" 用来传值得
	                      {id:1,username:zs}
	                   如果 Select 的绑定值为对象类型，请务必指定 value-key 作为它的唯一性标识。
					-->
					<el-select v-model="petAcquisitionOrderForm.manager" value-key="id" placeholder="请选择">
						<el-option
								v-for="item in employees"
								:key="item.id"
								:label="item.username"
								:value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="上级收购订单"  prop="parent">
					<!--可通过 props.checkStrictly = true 来设置父子节点取消选中关联，从而达到选择任意一级选项的目的。-->
					<!--:label 使用用来显示的-->
					<!--:value="item.id" 用来传值得-->
					<el-cascader
							v-model="petAcquisitionOrderForm.parent"
							:options="deptTree"
							:props="{ checkStrictly:true,label:'name',value:'id'}"
							clearable></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="petAcquisitionOrderFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
                page: 1,
				query: {
					keyword: ''
				},
				petAcquisitionOrders: [],
				employees:[],
				deptTree:[],
				total: 0,

				listLoading: false,
				sels: [],//列表选中列

				//添加获取编辑
				petAcquisitionOrderFormVisible: false,//编辑界面是否显示
				editLoading: false,
				petAcquisitionOrderFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' },
					],
					sn: [
						{ required: true, message: '请输入标识', trigger: 'blur' }
					]
				},
				//编辑界面数据
				petAcquisitionOrderForm: {
					id: null,
					name: '',
					sn: '',
                    dirPath: '',
                    state: 0,
                    manager: null,
                    parent: null
				}
			}
		},
		methods: {
            getDeptTree(){

                this.$http.get("/petAcquisitionOrder/tree")
                    .then(result=>{
                        this.deptTree = result.data;
                    })
            },
		    getEmployees(){

		        this.$http.get("/employee")
					.then(result=>{
					    this.employees = result.data;
					})
			},
			//性别显示转换
            // formatState: function (row, column) {
             //    if(row.state==0){
             //        return '启用';
             //    }
             //    return '<span style="color: red;">禁用</span>';
			// },
			handleCurrentChange(val) {
				this.page = val;
				this.getPetAcquisitionOrders();
			},
			//获取收购订单列表
            getPetAcquisitionOrders() {
				let para = {
                    currentPage: this.page,
					keyword: this.query.keyword
				};
				this.listLoading = true;

				this.$http.post("/petAcquisitionOrder",para)
					.then(result=>{
					    console.log(result)
					    this.petAcquisitionOrders = result.data.data;
					    this.total = result.data.total;
                        this.listLoading = false;
					})
					.catch(result=>{
					    alert(result);
					})
			},
			//删除
			handleDel: function (index, row) {
			    console.log(row);
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {
				    //确认后操作
					this.listLoading = true;

					///petAcquisitionOrder/1
					let url = "/petAcquisitionOrder/"+row.id
					this.$http.delete(url)
                        .then(result=>{
                            //关闭加载框
                            this.listLoading = false;
                            //提示删除成功 ajax success message
                            console.log(result);
                            result = result.data
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                            //从新加载数据
                            this.getPetAcquisitionOrders();
                        })

				}).catch(() => {
				    //取消后操作
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });

                });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			    //打开对话框
				this.petAcquisitionOrderFormVisible = true;
				this.getEmployees();
				this.getDeptTree();

				//回显 拷贝当前行数据给模型，这个模型绑定到form，由于双向绑定自动做回显
				this.petAcquisitionOrderForm = Object.assign({}, row);

				if(this.petAcquisitionOrderForm.parent){
                    let dirPath = row.dirPath
					console.log(dirPath) ///2/7/40
					if(dirPath){
						dirPath = dirPath.substr(1,dirPath.lastIndexOf("/")-1);
                        console.log(dirPath) // /2/7
                        let strs = dirPath.split("/");
                        let result = [];
                        for(let i=0;i<strs.length;i++){
                            result.push(parseInt(strs[i]))
						}
                        console.log(result)
                        this.petAcquisitionOrderForm.parent = result;
                        console.log( this.petAcquisitionOrderForm.parent)

					}

				}



			},
			//显示新增界面
			handleAdd: function () {
			    //打开对话框
				this.petAcquisitionOrderFormVisible = true;

				//清空数据
				this.petAcquisitionOrderForm = {
                    id: null,
                    name: '',
                    sn: '',
                    dirPath: '',
                    state: 0,
                    manager: null,
                    parent: null
                }

                this.getEmployees();
                this.getDeptTree();
                console.log(this.employees);
			},
			//编辑
            save: function () {
			    // 在form上面加了这个内容ref="petAcquisitionOrderForm"
				this.$refs.petAcquisitionOrderForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认保存吗？', '温馨提示', {}).then(() => {
							this.editLoading = true;

							//如果有必要在封装数据
							let para = Object.assign({}, this.petAcquisitionOrderForm);

							//级联狂获取是一个数组，需要自己封装数据
							if(this.petAcquisitionOrderForm.parent){
                                console.log(this.petAcquisitionOrderForm.parent)
                                let ids =  this.petAcquisitionOrderForm.parent;
                                para.parent = {
                                    id:ids[ids.length-1]
                                }
							}

							this.$http.put("/petAcquisitionOrder",para)
								.then((res) => {
									this.editLoading = false;
									res = res.data;
									if(res.success){
                                        this.$message({
                                            message: '保存成功',
                                            type: 'success'
                                        });
									}else{
                                        this.$message({
                                            message: res.message,
                                            type: 'error'
                                        });
                                    }
									this.$refs['petAcquisitionOrderForm'].resetFields();

									//关闭对话框
									this.petAcquisitionOrderFormVisible = false;
									//重新加载数据
									this.getPetAcquisitionOrders();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			    //[{id:1,name:zs},{.....}]
				var ids = this.sels.map(item => item.id);
				console.log(ids);
				this.$confirm('确认删除选中记录吗？', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
                    console.log(ids);
                    //axios get delete 不支持传对象
					this.$http.patch("/petAcquisitionOrder/bulk",ids)
						.then(result=>{
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getPetAcquisitionOrders();
						})
						.catch(result=>{
                            this.$message({
                                message: result.data.message,
                                type: "error"
                            });
						})
				}).catch(() => {
                    //取消后操作
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
				});
			}
		},
		mounted() {
			this.getPetAcquisitionOrders();
		}
	}

</script>

<style scoped>

</style>